<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\getdatas\MySubject;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Myadminuser extends BaseController
{
    public function show_list(Request $request){
        return Builder::table()
            ->setAction('myadminuser/page?model_id=1')
            ->addToolbarDemo('alldel','全部删除',[],'myadminuser/del')
            ->addToolbarDemo('add','添加',[],'myadminuser/show_add?model_id=1')
            ->setRightHeader()
            ->setMonitorAction('switch','myadminuser/change')
            ->setColumn('input','user','账号')
            ->setColumn('input','nickname','昵称')
            ->setColumn('input','role_id','角色')
            ->setColumn('input','phone','手机')
            ->setColumn('input','email','email')
            ->setColumn('img','portrait','头像')
            ->setColumn('input','pid','上级管理员')
            ->setColumn('switch','status','状态','myadminuser/change')
            ->setButton('detail','详情','myadminuser/detail')
            ->setButton('edit','修改','myadminuser/show_edit')
            ->setButton('add','添加','myadminuser/show_add')
            ->setButton('del','删除','myadminuser/del')
            ->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $data = Db::name(getTable('admin_user'))
            ->where('user',"<>",'root')
            ->paginate(10)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            if($value['pid']==0){
                $value['pid'] = '最上级管理员';
            }else{
                $shangji = Db::name(getTable('admin_user'))
                    ->where('id',$value['pid'])
                    ->find();
                if($shangji){
                    $value['pid'] = $shangji['nickname'];
                }else{
                    $value['pid'] = '';
                }
            }
            if($value['role_id']){
                $role = Db::name(getTable('roles'))
                    ->where('id',$value['role_id'])
                    ->find();
                if($role){
                    $value['role_id'] = $role['name'];
                }else{
                    $value['role_id'] = '';
                }
            }
            $value['portrait'] = getFilePath($value['portrait']);
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }
    public function show_add(Request $request){
        $id = $request->param('id',null);
        $user = session('adminuser');
        $array = Db::name(getTable('roles'))
            ->select()->toArray();
        return Builder::form()
            ->setAction('myadminuser/add')
            ->setProject('none','pid','上级',$id??$user['id'])
            ->setProject('input','user','账号')
            ->setProject('password','pwd','密码')
            ->setProject('input','nickname','昵称')
            ->setProject('select','role_id','角色','',$array,'',true)
            ->setProject('input','phone',' 手机')
            ->setProject('input','email',' 邮箱')
            ->setProject('img','portrait',' 头像')
            ->setProject('switch','status',' 状态',1)
            ->setButton('submit','提交')
            ->setButton('back')
            ->fetch();
    }
    public function add(Request $request){
        $data = $request->param();
        if($data['status']??null){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $user = Db::name(getTable('admin_user'))
            ->where('user',$data['user'])
            ->find();
        if($user){
            return Index::error('账号已存在');
        }
        if(!$data['pwd']??null){
            return Index::error('账号已存在');
        }
        $data['pwd'] = pwd_encode($data['pwd']);
        $newdata = [];
        $newdata['user'] = $data['user'];
        $newdata['nickname'] = $data['nickname'];
        $newdata['role_id'] = $data['role_id'];
        $newdata['phone'] = $data['phone'];
        $newdata['email'] = $data['email'];
        $newdata['status'] = $data['status'];
        $newdata['portrait'] = $data['portrait'];
        $newdata['pwd'] = $data['pwd'];
        $newdata['pid'] = $data['pid'];
        if(Db::name(getTable('admin_user'))->insert($newdata,true)){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }
    public function show_edit(Request $request){
        $id = $request->param('id');
        $array = Db::name(getTable('roles'))
            ->select()->toArray();
        $data = Db::name(getTable('admin_user'))
            ->where('id',$id)
            ->find();
//        echo $id;
//        exit;
        return Builder::form()
            ->setAction('myadminuser/edit')
            ->setProject('none','id','',$id)
            ->setProject('none','pid','上级',$data['pid'])
            ->setProject('input','user','账号',$data['user'])
            ->setProject('password','pwd','密码')
            ->setProject('input','nickname','昵称',$data['nickname'])
            ->setProject('select','role_id','角色',$data['role_id'],$array,'',true)
            ->setProject('input','phone',' 手机',$data['phone'])
            ->setProject('input','email',' 邮箱',$data['email'])
            ->setProject('img','portrait',' 头像',$data['portrait'])
            ->setProject('switch','status',' 状态',$data['status'])
            ->setButton('submit','提交')
            ->setButton('back')
            ->fetch();
    }
    public function change(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('admin_user'))
            ->where('id',$id)
            ->find();
        if($data['status']==1){
            $data['status'] = 0;
        }else{
            $data['status'] = 1;
        }
        if(Db::name(getTable('admin_user'))->where('id',$id)->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function edit(Request $request){
        $data = $request->param();
        if($data['status']??null){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $thisdata =Db::name(getTable('admin_user'))
            ->where('id',$data['id'])
            ->find();
        if($thisdata['user']!==$data['user']){
            $user = Db::name(getTable('admin_user'))
                ->where('user',$data['user'])
                ->find();
            if($user){
                return $this->error('账号已存在');
            }
        }
        $newdata = [];
        if($data['pwd']){
            $newdata['pwd'] = pwd_encode($data['pwd']);
        }
        $newdata['user'] = $data['user'];
        $newdata['nickname'] = $data['nickname'];
        $newdata['role_id'] = $data['role_id'];
        $newdata['phone'] = $data['phone'];
        $newdata['email'] = $data['email'];
        $newdata['status'] = $data['status'];
        $newdata['portrait'] = $data['portrait'];
        $newdata['pid'] = $data['pid'];
        $newdata['id'] = $data['id'];
        if(Db::name(getTable('admin_user'))->where('id',$newdata['id'])->update($newdata)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function detail(Request $request){
        $id = $request->param('id');
        $array = Db::name(getTable('roles'))
            ->select()->toArray();
        $data = Db::name(getTable('admin_user'))
            ->where('id',$id)
            ->find();
        return Builder::form()
            ->setAction('myadminuser/add')
            ->setProject('none','pid','上级',$data['pid'])
            ->setProject('input','user','账号',$data['user'])
            ->setProject('password','pwd','密码')
            ->setProject('input','nickname','昵称',$data['nickname'])
            ->setProject('select','role_id','角色',$data['role_id'],$array,'',true)
            ->setProject('input','phone',' 手机',$data['phone'])
            ->setProject('input','email',' 邮箱',$data['email'])
            ->setProject('img','portrait',' 头像',$data['portrait'])
            ->setProject('switch','status',' 状态',$data['status'])
//            ->setButton('submit')
            ->setButton('back')
            ->fetch();
    }
    public function del(Request $request){
        $id = $request->param('id');
        if(Db::name(getTable('admin_user'))->where('id','in',$id)->delete()){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}